<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎登陆</h2>
          <el-form-item label="" prop="username">
            <el-input
              placeholder="用户名"
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
              :show-password="true"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loadingStatus"
              class="login_btn"
              @click="loginClick"
            >
              提交
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'

//收集账号与密码的数据
let loginForm = reactive({ username: 'admin', password: '111111' })
let userSotre = useUserStore()
let $router = useRouter()
let loadingStatus = ref(false)
let loginForms = ref()
// 登陆按钮
let loginClick = async () => {
  // 判断表单验证是否通过
  await loginForms.value.validate()

  loadingStatus.value = true
  try {
    await userSotre.userLogin(loginForm)
    // 跳转
    $router.push('/')
    ElNotification.success({
      title: `${getTime()}好`,
      message: '登陆成功',
      showClose: false,
    })
    loadingStatus.value = false
  } catch (error) {
    ElNotification({
      type: 'error',
      // title:'提示',
      message: (error as Error).message,
      showClose: false,
    })
    loadingStatus.value = false
  }
}
// 表单校验
const rules = {
  username: { required: true, message: '请输入用户名', trigger: 'blur' },
  password: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 6, max: 10, message: '密码长度6至10', trigger: 'blur' },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.login_form {
  padding: 40px;
  position: relative;
  width: 80%;
  top: 30vh;
  background: url('@/assets/images/login_form.png') no-repeat;
  background-size: cover;

  h1 {
    color: white;
    font-size: 40px;
  }

  h2 {
    margin: 20px 0px;
    color: white;
    font-size: 20px;
  }
}

.login_btn {
  width: 100%;
}
</style>
